<template>
    <div class="page-tool" >
      <div class="page-tool-item" @click="lastPage">上一页</div>
      <div class="page-tool-item" @click="nextPage">下一页</div>
      <div class="page-tool-item">{{ state.pageNum }}/{{ state.numPages }}</div>
      <div class="page-tool-item" @click="pageZoomOut">放大</div>
      <div class="page-tool-item" @click="pageZoomIn">缩小</div>
    </div>
   
    <div class="pdf-wrap">
      <vue-pdf-embed :source="state.source" class="vue-pdf-embed" :page="state.pageNum" :style="scale" />
    </div>
  </template>
  <script setup lang="ts">
  import { reactive, onMounted, computed } from "vue";
  import VuePdfEmbed from "vue-pdf-embed";
  import { createLoadingTask } from "vue3-pdfjs";
   
  const props = defineProps({
    pdfUrl: {
      type: String,
      required: true
    }
  })
  const state = reactive({
    source: props.pdfUrl,// 预览pdf文件地址
    pageNum: 1,// 当前页面
    scale: 0.5, // 缩放比例
    numPages: 0, // 总页数
  });
  const scale = computed(() => `transform:scale(${state.scale})`)
  function lastPage() {
    if (state.pageNum > 1) {
      state.pageNum -= 1;
    }
  }
  function nextPage() {
    if (state.pageNum < state.numPages) {
      state.pageNum += 1;
    }
  }
  function pageZoomOut() {
      state.scale += 0.1;
  }
  function pageZoomIn() {
      state.scale -= 0.1;
  }
   
  onMounted(() => {
    const loadingTask = createLoadingTask(state.source);
    loadingTask.promise.then((pdf: { numPages: number }) => {
      state.numPages = pdf.numPages;
    });
  });
   
  </script>
  <style  scoped>
   
  .pdf-preview {
    position: relative;
    height: 100vh;
    padding: 20px 0;
    box-sizing: border-box;
    background: rgb(66, 66, 66);
  }
   
  .vue-pdf-embed {
    text-align: center;
    margin: 0 auto;
    box-sizing: border-box;
    transform-origin: top center; /* Set transform origin */
  }
   
  .pdf-preview {
    position: relative;
    height: 100vh;
    padding: 20px 0;
    box-sizing: border-box;
    background-color: #e9e9e9;
  }
   
  .pdf-wrap {
    overflow: auto;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #e9e9e9;
  }
   
  .vue-pdf-embed {
    text-align: center;
    margin: 0 auto;
    box-sizing: border-box;
    transform-origin: top center;
    width: 100%;
    height: 100%;
    max-height: 100vh; 
  }
   
  .page-tool {
    position: fixed; /* Fix the position of the toolbar */
    bottom: 35px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    background: rgb(66, 66, 66);
    color: white;
    border-radius: 19px;
    z-index: 100;
    cursor: pointer;
  }
   
  .page-tool-item {
    padding: 8px 15px;
    padding-left: 10px;
    cursor: pointer;
   
  }


  .page-tool-item:hover{
      background-color: grey;
    }
  </style>